package com.cloud.common.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
 
public class RestClientUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(RestClientUtil.class);
 
	public static <T> T httpClientByPost(String url, Object request, Class<T> responseType) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		HttpEntity<Object> strEntity = new HttpEntity<Object>(request, headers);
		RestTemplate restTemplate=new RestTemplate();
		logger.info("httpClientByPost:" + url);
		return (T) restTemplate.postForObject(url, strEntity, responseType);
	}
 
    public static <T> T httpClientByPost(String url, Object request, Class<T> responseType, MediaType mediaType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType);
        HttpEntity<Object> strEntity = new HttpEntity<Object>(request, headers);
        RestTemplate restTemplate = new RestTemplate();
        logger.info("httpClientByPost:" + url);
        return (T) restTemplate.postForObject(url, strEntity, responseType);
 
    }
 
	public static <T> T httpClientByGet(String url, Class<T> responseType) {
		logger.info("httpClientByGet:" + url);
		RestTemplate restTemplate=new RestTemplate();
		return (T) restTemplate.getForObject(url, responseType);
	}
 
}